﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ELGGAPI.Models;
using ELGGAPI.Data;

namespace ELGGAPI.Controllers
{
    public class GameInstanceController : ApiController
    {
        public Response<GUIDclass> PostManageGameInstance(Request<GameInstanceModels> request)
        {
            var response = new Response<GUIDclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {

                    string strInstanceGUID = GameDomainModel.AddUpdateGameInstance(request.Data);
                    if (strInstanceGUID == "-100")
                    {
                        response.Message = "Invalid Game id";
                        return response;
                    }
                    else
                    {
                        response.Data = new GUIDclass { guid = strInstanceGUID };
                    }

                }

                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }

        public Response<GUIDclass> PostDeleteGameInstance(Request<GUIDclass> request)
        {
            var response = new Response<GUIDclass>();
            try
            {
                var user = AutheticationToken.ValidateToken(request.access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    string strInstanceGUID = GameDomainModel.DeleteGameInstance(request.Data.guid);
                    if (strInstanceGUID == "-100")
                    {
                        response.Message = "Invalid Game id";
                        return response;
                    }
                    else
                    {
                        response.Data = new GUIDclass { guid = strInstanceGUID };
                    }
                }

                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
        public Response<IEnumerable<GameInstanceModels>> GetGameInstance(string gameinstanceid,string action,string palyerid, string access_token)
        {
            var response = new Response<IEnumerable<GameInstanceModels>>();
            try
            {
                var user = AutheticationToken.ValidateToken(access_token);
                if (user == null)
                {
                    response.Data = null;
                    response.Message = "Token is invalid";
                    return response;
                }
                else
                {
                    bool pAction=false;
                    if(!string.IsNullOrEmpty(action))
                    {
                        if(action=="1")
                            pAction=true;
                    }
                    int intplayerid = 0;
                    if (!string.IsNullOrEmpty(palyerid))
                    {
                        intplayerid = int.Parse("0"+palyerid);
                    }
                    
                    response.Data = GameDomainModel.GetGameInstanceList(gameinstanceid, pAction, intplayerid);
                   
                }

                return (response);
            }
            catch (Exception ex)
            {
                response.Message = ex.Message;
                return response;
            }
        }
    }
}
